import { sdk } from '../../config/sdk';
import api from '../../api/index';
Page({
  data: {
    PageCur: 'home',
    sectionIndex: wx.getStorageSync('sectionIndex') || [4, 3, 1],
    currentWeek: 0, // 开学周数
    course: [], // 课表
  },

  onLoad: function () {
    this.getData();
  },

  navTo(e) {
    this.setData({
      PageCur: e.currentTarget.dataset.cur
    });
  },

  async getData(refresh = false) {
    const { year, term } = sdk.isDate.getDate();
    const { code, message, data } = await sdk.request({ url: api.getCourse, data: { year, term, refresh } });
    if (code != 1000) return this.$error(message);
    const remind = wx.getStorageSync('remind') == 1 ? true : false;
    if (refresh) wx.stopPullDownRefresh();
    const { course, startDate } = data;
    this.setData({
      course,
      startDate,
      remind
    });
    this.getCourse(course);
  },

  // 获取当天课表
  getCourse(course) {
    let todayCourse = new Object;
    let morning = [];
    let afternoon = [];
    let night = [];
    const today = new Date().getDay() == 0 ? 7 : new Date().getDay();
    const currentWeek = sdk.isDate.getWeek(this.data.startDate);
    for (let i = 0; i < course.length; i++) {
      // 判断课表 周数 日期 是否与今天相等
      for (let j = 0; j < course[i].weeks.length; j++) {
        if (currentWeek == course[i].weeks[j] && course[i].day == today) {
          let am, pm, en = null;
          for (let s = course[i].sections[0]; s <= course[i].sections[1]; s++) {
            let data = JSON.parse(JSON.stringify(course[i]));
            let grid = JSON.parse(JSON.stringify(this.properties.sectionIndex));
            grid[0] = grid[0] + 1;
            grid[1] = grid[1] + 1;
            grid[2] = grid[2] + 1;
            if (s <= grid[0] && s < grid[0] + 1) {
              data.sections[1] = course[i].sections[1] > grid[0] ? grid[0] : course[i].sections[1];
              am = data;
            }
            if (s >= grid[0] + 1 && s < grid[0] + grid[1] + 1) {
              data.sections[0] = course[i].sections[0] < grid[0] + 1 ? grid[0] + 1 : course[i].sections[0];
              data.sections[1] = course[i].sections[1] > grid[0] + grid[1] ? grid[0] + grid[1] : course[i].sections[1];
              pm = data;
            }
            if (s > grid[0] + grid[1]) {
              data.sections[0] = course[i].sections[0] < grid[0] + grid[1] + 1 ? grid[0] + grid[1] + 1 : course[i].sections[0];
              en = data;
            }
          }
          if (am != null) {
            morning.push(am);
          }
          if (pm != null) {
            afternoon.push(pm);
          }
          if (en != null) {
            night.push(en);
          }
        };
      };
    };
    todayCourse.morning = morning;
    todayCourse.afternoon = afternoon;
    todayCourse.night = night;
    this.setData({
      currentWeek,
      todayCourse
    });
  },

  // 日期选择
  async dateChange(e) {
    const currentWeek = sdk.isDate.getWeek(e.detail);
    const { code, message } = await sdk.request({ url: api.updateDate, data: { date: e.detail } });
    if (code != 1000) return this.$error(message);
    this.setData({
      startDate: e.detail,
      currentWeek
    });
  },
  // 节数设置
  sectionChange(e) {
    this.setData({
      sectionIndex: e.detail
    });
  },
  // 课表提醒
  async remind(e) {
    const { code, message } = await sdk.request({ url: api.remind, data: { remind: e.detail == true ? 1 : 0 } });
    if (code != 1000) return this.$error(message);
    this.setData({ 
      remind: e.detail 
    });
  },

  // 监听用户下拉动作
  onPullDownRefresh: function () {
    this.getData(true);
  }
})